package tas.co.in.daoimpl.transactions;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import tas.co.in.dao.transactions.TransactionApprovalConect;
import tas.co.in.domain.transactions.Transactions;

public class TransactionApprovalsImpl extends SqlMapClientDaoSupport implements
		TransactionApprovalConect {

	TransactionTemplate template = null;

	public void setTemplate(TransactionTemplate template) {
		this.template = template;
	}

	@SuppressWarnings("unchecked")
	public List<Transactions> getTransactionRequests(Transactions transactions) {

		List<Transactions> payList = new ArrayList<Transactions>();
		List<Transactions> recList = new ArrayList<Transactions>();
		

		try {
			payList = getSqlMapClient().queryForList(
					"transactions.getpayrequests",transactions);
			recList = getSqlMapClient().queryForList(
					"transactions.getrecrequests",transactions);

			payList.addAll(recList);

		} catch (Exception e) {
			System.out.println(e);
		}
		return payList;
	}

	public boolean approveTransactions(final StringTokenizer tokenizer,
			final Transactions inputBean) {

		boolean bool;

		bool = (Boolean) template.execute(new TransactionCallback() {

			public Boolean doInTransaction(TransactionStatus status) {

				boolean resultFlag = false;
				Transactions transactions = new Transactions();
				Transactions transactions2 = new Transactions();
				Transactions transactions3 = new Transactions();
				Transactions companyInfo = new Transactions();
				Transactions toCustomerInfo = new Transactions();
				Object savePoint = null;
				float fromCustomerValue = 0;
				float toCustomerValue = 0;

				while (tokenizer.hasMoreTokens()) {

					transactions.setFromNumber(tokenizer.nextToken());
					transactions.setToNumber(tokenizer.nextToken());
					transactions.setAmount(Float.parseFloat(tokenizer
							.nextToken()));
					transactions.setVoucherType(tokenizer.nextToken());
					transactions.setDate(tokenizer.nextToken());
					transactions.setServiceCharge(tokenizer.nextToken());
					transactions.setTransactionId(Integer.parseInt(tokenizer
							.nextToken()));
					transactions.setCompanyNumber("8687944644");

					try {
						transactions2 = (Transactions) getSqlMapClient()
								.queryForObject(
										"transactions.getFromCustomerInfo",
										transactions);

						transactions.setCustomerId(transactions2
								.getCustomerId());
						transactions.setCustomerName(transactions2
								.getCustomerName());
						transactions.setLedgerNumber(transactions2
								.getLedgerNumber());
						transactions.setFromCustomerAccountBal(transactions2
								.getFromCustomerAccountBal());

						toCustomerInfo = (Transactions) getSqlMapClient()
								.queryForObject(
										"transactions.getToCustomerInfo",
										transactions);

						companyInfo = (Transactions) getSqlMapClient()
								.queryForObject("transactions.getCompanyInfo",
										transactions);

						transactions3 = (Transactions) getSqlMapClient()
								.queryForObject("transactions.getvouchernumber");

						transactions.setVoucherNumber(transactions3
								.getVoucherNumber());

						companyInfo.setServiceCharge(transactions
								.getServiceCharge());
						companyInfo.setVoucherNumber(transactions
								.getVoucherNumber());
						companyInfo.setVoucherType(transactions
								.getVoucherType());
						companyInfo.setCompanyNumber("8687944644");
						companyInfo.setUserId(inputBean.getUserId());

						transactions.setToCustomerId(toCustomerInfo
								.getToCustomerId());
						transactions.setToCustomerName(toCustomerInfo
								.getToCustomerName());
						transactions.setToCustomerLedger(toCustomerInfo
								.getToCustomerLedger());
						transactions.setToCustomerAccountBal(toCustomerInfo
								.getToCustomerAccountBal());
						transactions.setUserId(inputBean.getUserId());

						savePoint = status.createSavepoint();

						if (transactions.getVoucherType().equalsIgnoreCase("p")) {

							getSqlMapClient().insert(
									"transactions.insertpaycreditrecord",
									transactions);
							getSqlMapClient().insert(
									"transactions.insertpaydebitrecord",
									transactions);

							fromCustomerValue = transactions
									.getFromCustomerAccountBal()
									- (transactions.getAmount() + Float
											.parseFloat(transactions
													.getServiceCharge()));
							toCustomerValue = transactions
									.getToCustomerAccountBal()
									+ transactions.getAmount();

						} else if (transactions.getVoucherType()
								.equalsIgnoreCase("r")) {

							getSqlMapClient().insert(
									"transactions.insertrecdebitrecord",
									transactions);

							getSqlMapClient().insert(
									"transactions.insertreccreditrecord",
									transactions);

							fromCustomerValue = transactions
									.getFromCustomerAccountBal()
									+ (transactions.getAmount() - Float
											.parseFloat(transactions
													.getServiceCharge()));
							toCustomerValue = transactions
									.getToCustomerAccountBal()
									- transactions.getAmount();
						}

						getSqlMapClient().insert(
								"transactions.insertservicecreditrecord",
								transactions);

						getSqlMapClient().insert(
								"transactions.insertservicedebitrecord",
								companyInfo);

						transactions.setToCusotmerAmount(toCustomerValue);

						transactions
								.setFromcustomerDeduction(fromCustomerValue);

						getSqlMapClient().update(
								"transactions.updatefromcustomerbalance",
								transactions);

						if (transactions.getFromNumber().equalsIgnoreCase(
								"8687944644")) {

							companyInfo = (Transactions) getSqlMapClient()
									.queryForObject(
											"transactions.getCompanyInfo",
											transactions);

							companyInfo.setServiceCharge(transactions
									.getServiceCharge());
							companyInfo.setVoucherNumber(transactions
									.getVoucherNumber());
							companyInfo.setVoucherType(transactions
									.getVoucherType());
							companyInfo.setCompanyNumber("8687944644");
							companyInfo.setUserId(inputBean.getUserId());

						}

						getSqlMapClient().update(
								"transactions.updatetocustomerbalance",
								transactions);
						getSqlMapClient().update(
								"transactions.updatecompanybalance",
								companyInfo);
						getSqlMapClient().update(
								"transactions.updatefromledgerdetails",
								transactions);
						getSqlMapClient().update(
								"transactions.updatetoledgerdetails",
								transactions);
						getSqlMapClient().update(
								"transactions.updatecompanyledgerdetails",
								companyInfo);

						if (transactions.getVoucherType().equalsIgnoreCase("r")) {

							getSqlMapClient().update(
									"transactions.updaterecbufferrecord",
									transactions);

						} else if (transactions.getVoucherType()
								.equalsIgnoreCase("p")) {

							getSqlMapClient().update(
									"transactions.updatepaybufferrecord",
									transactions);

						}

						resultFlag = true;

					} catch (Exception e) {

						System.out.println(e);

						status.rollbackToSavepoint(savePoint);

					}

				}

				return resultFlag;
			}

		});
		return bool;
	}

}
